package hivi.vn.hivigold.activities;

import hivi.vn.hivigold.R;
import hivi.vn.hivigold.database.Preferences;
import hivi.vn.hivigold.database.WebService;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivities extends Activity {
	Button btnLogin;
	CheckBox chkRemember;
	EditText txtUsername;
	EditText txtPassword;
	boolean flag =true;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login_layout);
		
		if(!WebService.Test(this))
		{
			Intent i = new Intent(this, SettingActivities.class);
			startActivity(i);
		}
		findControls();
		defaultValue();
		hideKeyboard();
	}
	void showKeyboard() {
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm != null)
		{
			imm.showSoftInput(txtUsername, 0);
		}
			
	}

	void hideKeyboard() {
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		if (imm != null)
			imm.hideSoftInputFromWindow(txtUsername.getWindowToken(), 0);
	}
	
	// Event
	OnClickListener onClick = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(v.getId()==R.id.btnLogin)
			{
				if(checkLogin())
				{
					Intent i = new Intent(getBaseContext(), Main.class);
					startActivity(i);
					finish();
				}
			}
		}
	};
	
	// Method
	private void findControls()
	{
		btnLogin = (Button) findViewById(R.id.btnLogin);
		chkRemember = (CheckBox) findViewById(R.id.chkRemember);
		txtUsername = (EditText) findViewById(R.id.txtUsername);
		txtPassword = (EditText) findViewById(R.id.txtPassword);
		btnLogin.setFocusable(true);
		btnLogin.setOnClickListener(onClick);
	}
	
	private void defaultValue()
	{
		String username = Preferences.getPreferenceValue(this, Preferences.USERNAME);
		if(username.equals("*"))
		{
			return;
		}
		String password = Preferences.getPreferenceValue(this, Preferences.PASSWORD);
		txtUsername.setText(username);
		txtPassword.setText(password);
		chkRemember.setChecked(true);
		
	}
	
	private boolean checkLogin() {
		if( chkRemember.isChecked())
		{
			Preferences.editPreferenceValue(this, Preferences.USERNAME, txtUsername.getText().toString());
			Preferences.editPreferenceValue(this, Preferences.PASSWORD, txtPassword.getText().toString());
		}else
		{
			Preferences.editPreferenceValue(this, Preferences.USERNAME, "*".toString());
			Preferences.editPreferenceValue(this, Preferences.PASSWORD, "*".toString());
		}
		if( txtUsername.getText().toString().trim().length()==0)
		{
			Toast.makeText(this, this.getResources().getString(R.string.warning_user_notempty), Toast.LENGTH_LONG).show();
			return false;
		}
		if(!WebService.checkLogin(this,txtUsername.getText().toString(), txtPassword.getText().toString()))
		{
			Toast.makeText(this, this.getResources().getString(R.string.warning_login_notvalid), Toast.LENGTH_LONG).show();
			return false;	
		}
		
		return true;
	}
}
